<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="账号设置">
            <el-divider>
              <h3>账号设置</h3>
            </el-divider>
            <!-- 放置表单 -->
            <el-form
              ref="bbc"
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
              :model="userInfo"
              :rules="rules"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width: 300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="newPassword">
                <el-input
                  v-model="userInfo.newPassword"
                  style="width: 300px"
                  type="password"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleUpdate">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <el-tooltip class="tooltip-box" content="打印基本个人信息">
              <router-link
                :to="`/employees/print/${$route.params.id}?type=personal`"
              >
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
            <!-- 内容 -->
            <user-info />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <el-tooltip class="tooltip-box" content="打印基本岗位信息">
              <router-link
                :to="`/employees/print/${$route.params.id}?type=job`"
              >
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
            <!-- 内容 -->
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getEmplyeeInfoAPI, updateEmployeeAPI } from '@/api/employees'
import userInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
export default {
  name: 'EmployeesDetail',
  components: { userInfo, JobInfo },
  data() {
    return {
      userInfo: {
        username: '',
        newPassword: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '姓名不能为空',
            trigger: ['blur', 'change']
          }
        ],
        newPassword: [
          {
            min: 6,
            max: 9,
            message: '密码必须是6-9位',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  created() {
    this.getEmplyeeInfo()
  },

  methods: {
    // 获取员工基本信息
    async getEmplyeeInfo() {
      const { data } = await getEmplyeeInfoAPI(this.$route.params.id)
      //   console.log(data)
      //   赋值--回显
      this.userInfo = data
    },
    // 修改员工基本信息
    handleUpdate() {
      // eslint-disable-next-line space-before-function-paren
      this.$refs.bbc.validate(async (value) => {
        if (!value) return
        try {
          await updateEmployeeAPI({
            ...this.userInfo,
            password: this.userInfo.newPassword
          })
          this.$message.success('更新成功')
        } catch (error) {
          this.$message.error(error)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.employees-detail-container {
  .el-tab-pane {
    padding-top: 10px;
  }
  .tooltip-box {
    position: absolute;
    right: 30px;
    top: 10px;
    z-index: 999;
  }
}
</style>
